package com.cheyun.netsalev3.view

import android.content.Intent
import android.util.Log
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProviders
import com.cheyun.netsalev3.R
import com.cheyun.netsalev3.adapter.MyAdapter
import com.cheyun.netsalev3.bean.CustomListParam
import com.cheyun.netsalev3.databinding.ActivityCustomListBinding
import com.cheyun.netsalev3.utils.BaseActivity
import com.cheyun.netsalev3.viewmodel.CustomListActivityViewModel
import java.io.Serializable

class CustomListActivity : BaseActivity<ActivityCustomListBinding>() {
    override fun initContentView(): Int {
        return R.layout.activity_custom_list
    }

    override fun initViewModel(): ViewModel? {
        return ViewModelProviders.of(this).get(CustomListActivityViewModel::class.java)
    }

    override fun initData() {
        var lists = intent.getSerializableExtra("list") as ArrayList<CustomListParam>
        binding.viewModel?.setdata(lists)
        subscribeUi()
    }


    private lateinit var adapter: MyAdapter

    override fun initView() {

        adapter = MyAdapter(R.layout.custom_list_adapter_item, binding.viewModel)
        binding.recyclerView.adapter = adapter
        binding.queDingBut.setOnClickListener {
            var intent = Intent()
            binding.viewModel?.lists?.value.apply {
                intent.putExtra("poplist", binding.viewModel?.lists?.value as Serializable)
            }
            setResult(101, intent)
            finish()
        }
        binding.quXiaoBut.setOnClickListener {
            finish()
        }
    }

    private fun subscribeUi() {
        binding.viewModel?.lists?.observe(this, Observer { lists ->
            Log.e("subscribeUi", "............")
            adapter.submitList(lists)
        })

    }
}
